package com.rrd.hive.udf.sms;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.ql.exec.UDF;

import java.util.List;


public class SmsParserBaseUDF extends UDF {

    public static Configuration config() {
        System.setProperty("HADOOP_USER_NAME", "hdfs");
        Configuration conf = new Configuration();
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        return conf;
    }

    public String getPattern(String compareContent, String smsBody, List<CompareTemplate> templateList) {
        String resPattern = null;

        for (CompareTemplate template: templateList) {
            String compareType = template.getCompareType();
            String tmpCmpContent = template.getCompareContent();
            boolean cmpResult = false;
            
            if("equal".equals(compareType)) {
                cmpResult = tmpCmpContent.equals(compareContent);
            } else if ("rlike".equals(compareType)) {
                cmpResult = (HiveUdfRegexTools.hiveUdfRegExpEx(compareContent, tmpCmpContent) == 1 ? true : false);
            } else {
                System.out.println("error compareType: " + compareType);
                return null;
            }

            if(cmpResult) {
                if(template.getConditions() == null) {
                    resPattern = template.getPattern1();
                    break;
                } else {
                    if(HiveUdfRegexTools.hiveUdfRegExpEx(smsBody, template.getConditions()) == 1) {
                        resPattern = template.getPattern1();
                        break;
                    } else {
                        resPattern = template.getPattern2();
                        break;
                    }
                }
            }
        }
        return resPattern;
    }

    private static class CompareTemplate {
        private String compareType;
        private String compareContent;
        private String conditions;
        private String pattern1;
        private String pattern2;


        public CompareTemplate(String compareType, String compareContent, String conditions, String pattern1, String pattern2) {
            this.compareType = compareType;
            this.compareContent = compareContent;
            this.conditions = conditions;
            this.pattern1 = pattern1;
            this.pattern2 = pattern2;
        }

        public String getCompareType() {
            return compareType;
        }

        public String getCompareContent() {
            return compareContent;
        }

        public String getConditions() {
            return conditions;
        }

        public String getPattern1() {
            return pattern1;
        }

        public String getPattern2() {
            return pattern2;
        }

        @Override
        public String toString() {
            return "CompareTemplate{" +
                    "compareType='" + compareType + '\'' +
                    ", compareContent='" + compareContent + '\'' +
                    ", conditions='" + conditions + '\'' +
                    ", pattern1='" + pattern1 + '\'' +
                    ", pattern2='" + pattern2 + '\'' +
                    '}';
        }
    }

}
